// 准入机构

import { getData } from '@vimi/utils-api'
import { addressCut } from '@vimi/utils-tool'
const getFirstData = res => {
  return (res && res.data && res.data.length && res.data[0]) || {}
}

const state = {
  provinceAccessAgency: {},
  provinceCreditAmount: 0,

  // 准入我的合作机构
  accessAgency: [],

  // 省担保合作机构
  accessCreditAgency: [],

  accessGovAgency: [],
  accessBankAgency: [],

  // 所有银行
  allAccessBankAgency: [],
}

const getters = {
  provinceAccessAgency: state => state.provinceAccessAgency,
  provinceCreditAmount: state => state.provinceCreditAmount,
  accessAgency: state => state.accessAgency,
  accessCreditAgency: state => state.accessCreditAgency,
  accessGovAgency: state => state.accessGovAgency,
  accessBankAgency: state => state.accessBankAgency,
  allAccessBankAgency: state => state.allAccessBankAgency,
}

const actions = {
  async getProvinceAccessAgency({ commit, rootGetters }, force = false) {
    if (!force && state.provinceAccessAgency.credit_agency_access_id) {
      return Promise.resolve(state.provinceAccessAgency)
    }
    const res = await getData('credit', 'credit_agency_access_view', {
      'service_agency_lever.eq': 1,
      'access_status.in': '1,3',
      'access_agency_id.eq': rootGetters.clientId,
    })
    const data = getFirstData(res)

    data.authorized_amount = +(data.authorized_amount || 0)
    data.credit_amount = +(data.credit_amount || 0)

    commit('provinceAccessAgency', data)
    commit('provinceCreditAmount', data.authorized_amount)
    return data
  },

  async getAccessAgency({ commit, rootGetters }, force = false) {
    if (!force && state.accessAgency.length) {
      return Promise.resolve(state.accessAgency)
    }
    const res = await getData('credit', 'credit_agency_access_view', {
      'access_status.in': '1,3',
      'access_agency_id.eq': rootGetters.clientId,
    })

    const data = (res && res.data) || []
    commit('accessAgency', data)
    return data
  },

  async getAccessCreidtAgency({ commit, rootGetters }, force = false) {
    if (!force && state.accessCreditAgency.length) {
      return Promise.resolve(state.accessCreditAgency)
    }
    const res = await getData('credit', 'credit_agency_access_view', {
      'agency_type.eq': 6,
      // 'access_status.in': '1,3',
      'service_id.eq': rootGetters.clientId,
    })
    const data = (res && res.data) || []
    commit('accessCreditAgency', data)
    return data
  },

  async getAccessGovAgency({ commit, rootGetters }, force = false) {
    if (!force && state.accessGovAgency.length) {
      return Promise.resolve(state.accessGovAgency)
    }
    const res = await getData('credit', 'credit_agency_access_view', {
      'agency_type.eq': 3,
      'orWhere.address_code.endlike': addressCut(rootGetters.user.queryAddressCode),
      'orWhere.service_id.eq': rootGetters.clientId,
    })
    const data = (res && res.data) || []
    commit('accessGovAgency', data)
    return data
  },

  async getAccessBankAgency({ commit, rootGetters }, force = false) {
    if (!force && state.accessBankAgency.length) {
      return Promise.resolve(state.accessBankAgency)
    }
    const res = await getData('credit', 'credit_agency_access_view', {
      'agency_type.eq': 1,
      'service_id.eq': rootGetters.clientId,
    })
    const data = (res && res.data) || []
    commit('accessBankAgency', data)
    return data
  },

  async getAllAccessBankAgency({ commit }, params = {}) {
    const res = await getData('credit', 'credit_agency_info_view', {
      'agency_type.eq': 1,
      fields: [
        'agency_name',
        'agency_id',
        'parent_code',
        'agency_code',
        'address',
        'address_code',
        'address_detail',
      ],
      ...params,
    })
    const data = (res && res.data) || []
    commit('allAccessBankAgency', data)
    return data
  },
}

const mutations = {
  provinceAccessAgency: (state, payload) => (state.provinceAccessAgency = payload),
  provinceCreditAmount: (state, payload) => (state.provinceCreditAmount = payload),
  accessAgency: (state, payload) => (state.accessAgency = payload),
  accessCreditAgency: (state, payload) => (state.accessCreditAgency = payload),
  accessGovAgency: (state, payload) => (state.accessGovAgency = payload),
  accessBankAgency: (state, payload) => (state.accessBankAgency = payload),
  allAccessBankAgency: (state, payload) => (state.allAccessBankAgency = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
